<template>
  <div class="card-box">
    <v-dialog :show.sync="dialogVisible" @beforeClose="beforeClose" width="70%" title="授权物业信息">
      <div v-if="dialogVisible">
        <el-radio-group v-model="radio1" style="width: 200px;">
          <el-radio label="0" value="0">不限</el-radio>
          <el-radio label="1">未授权</el-radio>
        </el-radio-group>

        <el-input v-model="keyword" placeholder="物业名称" style="width: 200px;"></el-input>
        <el-button type="primary" size="small" @click="searchProperty" style="width: 80px" plain>搜索</el-button>

        <list-page
          v-loading="loading"
          :showSearch="false"
          :pager="pager"
          :data="tableData"
          @selectionChange="selectionChange"
          @changePageSize="changePageSize"
          @changeCurrentPage="changeCurrentPage"
        >
          <template slot="buttons"></template>
          <template slot="table">
            <el-table :data="tableData" :row-style="{height: '40px'}" align="center" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55"></el-table-column>
              <el-table-column prop="propertyId" label="物业编号"></el-table-column>
              <el-table-column prop="propertyName" label="物业名称"></el-table-column>
              <el-table-column prop="propertyRightStr" label="物业产权"></el-table-column>
              <el-table-column prop="propertyAddress" label="物业地址"></el-table-column>
              <el-table-column prop="authorizationStatus" :formatter="dictVal_authStart" label="授权状态"></el-table-column>
            </el-table>
          </template>
        </list-page>
      </div>
      <div slot="foot">
        <el-button type="primary" size="small" v-throttle="[toDetermine]" style="width: 80px">确定</el-button>
        <el-button type="primary" size="small" @click="dialogVisible = false" style="width: 80px">关闭</el-button>
      </div>
    </v-dialog>
  </div>
</template>

<script>
import { getPropertyList } from "@/views/authorization-management/use-card/api/use-card.js";
import pager from '@/assets/js/page-mixin';
import { getDict } from "~/utils";

export default {
  mixins: [pager],
  data () {
    return {
      loading: true,
      authStart: [],
      //分页数据
      pager: pager,
      //弹出框 初始时隐藏
      dialogVisible: false,
      multipleSelection: [],
      radio1: "0",
      keyword: "",
      tableData: [
        //     {
        //     num: 1,
        //     propertyId: 'SDxxsdweqwe',
        //     propertyName: 'xxxxxxx单元',
        //     propertyAddress: '深南大道200号',
        //     addressArea: "122.46",
        //     startTime: "2007-01-09",
        //     endTime: "2027-01-15",
        //     // authorizationUse: "产业用房",
        //     // practicalUse: "行政办公",
        //     useStatus:1,
        //     authorizationStatus: 0,
        //     remarks: "xxxx"
        // }
      ]


    }
  },
  created () {
    this.authStart = getDict('property-auth-status');
  },
  //载入事件
  mounted: function () {
    this.query();
  },
  watch: {
    'radio1': function () {
      //do something
      this.searchProperty()
    },
  },
  methods: {
    /**
     * 示新增弹框
     */
    show (rowData) {
      this.dialogVisible = true
    },

    /**
     * 关闭弹窗
     * @param done
     */
    beforeClose (done) {
      this.dialogVisible = false
      done();
    },

    /**
     * 每页显示条数
     * @param pageSize
     */
    changePageSize (pageSize) {
      this.pager.pageSize = pageSize;
      this.query();

    },
    /**
     * 分页跳转
     * @param current
     */
    changeCurrentPage (current) {
      console.log(current);
      this.pager.currentPage = current;
      this.query();
    },

    /**
     * 给选择的行赋值
     */
    handleSelectionChange (val) {
      this.multipleSelection = val;
    },

    /**
     *  把选择的行添加到父页面
     */
    toDetermine () {
      this.dialogVisible = false;
      //添加物业记录到主页面
      if (null != this.multipleSelection && this.multipleSelection.length > 0) {
        for (let i = 0; i < this.multipleSelection.length; i++) {
          //如果存在重复的选择，则不添加
          if (null != this.$parent.formObj.useCardPropertyList && this.$parent.formObj.useCardPropertyList.length > 0) {
            for (let k = 0; k < this.$parent.formObj.useCardPropertyList.length; k++) {
              if (this.multipleSelection[i].roomId == this.$parent.formObj.useCardPropertyList[k].roomId) {
                this.multipleSelection.splice(i, 1);
              }
            }
          }
        }
        for (let i = 0; i < this.multipleSelection.length; i++) {
          this.$parent.formObj.useCardPropertyList.push(this.multipleSelection[i]);
        }
      }
    },

    /**
     * 查询方法
     * @param param
     */
    searchProperty () {
      this.loading = true;
      this.query();
    },
    /**
     * 查询方法
     * @param param
     */
    query () {
      this.loading = true;
      getPropertyList(this.keyword, pager.currentPage, pager.pageSize, this.radio1).then(respData => {
        this.tableData = respData.data.data.records;
        this.pager.total = respData.data.data.total;
        this.loading = false;
      }).catch(error => {
        this.$message({
          type: 'error',
          message: '查询失败'
        });
      }).finally(() => (this.loading = false));
    },

    /**
     * 码值转换
     * @param row
     * @param column
     * @param cellValue
     * @returns {string|*}
     */
    dictVal_authStart: function (row, column, cellValue) {
      let obj = this.authStart.find(p => p.value == cellValue)
      if (obj) {
        return obj.label
      }
      return ''
    },


  }


}
</script>

<style scoped lang='scss'>
@include formWrap();
</style>
